const axios = require('axios');

const baseURL = 'http://localhost:7002/api/v1';

// 测试用的token
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJkZW1vIiwiZW1haWwiOiJkZW1vQGV4YW1wbGUuY29tIiwiaWF0IjoxNzUzMjM0MTc1LCJleHAiOjE3NTM4Mzg5NzV9.eH9yAWLfyrJVCUB-DqhI1Z5aaxtiED72oRZTESf-7Qs';

const api = axios.create({
  baseURL,
  headers: {
    'Authorization': `Bearer ${token}`,
    'Content-Type': 'application/json'
  }
});

async function testBlueColorRecommendation() {
  console.log('🔵 测试蓝色颜色推荐修复效果...\n');

  try {
    // 1. 测试蓝色单色搭配
    console.log('1️⃣ 测试蓝色单色搭配...');
    try {
      const blueMonoRes = await api.get('/recommendation/color-based', {
        params: { 
          baseColor: '蓝色', 
          colorScheme: 'monochromatic' 
        }
      });
      
      console.log(`✅ 蓝色单色推荐成功，获得 ${blueMonoRes.data.data.recommendations?.length || 0} 个推荐`);
      
      if (blueMonoRes.data.data.recommendations?.length > 0) {
        blueMonoRes.data.data.recommendations.forEach((rec, index) => {
          console.log(`\n   推荐 ${index + 1}: ${rec.name || '无名称'}`);
          console.log(`   推荐理由: ${rec.reason || '无'}`);
          console.log(`   包含 ${rec.items?.length || 0} 件衣服:`);
          
          let hasBlueItem = false;
          rec.items?.forEach(item => {
            const category = item.category?.name || '未知分类';
            const isBlue = item.color && item.color.includes('蓝');
            if (isBlue) hasBlueItem = true;
            
            console.log(`     ${isBlue ? '🔵' : '⚪'} ${item.name}(${item.color || '无色'}) [${category}]`);
          });
          
          if (hasBlueItem) {
            console.log(`   ✅ 包含蓝色单品`);
          } else {
            console.log(`   ❌ 没有蓝色单品！`);
          }
        });
      } else {
        console.log('   ⚠️ 没有生成推荐');
      }
    } catch (error) {
      console.error('❌ 蓝色单色推荐失败:', error.response?.data?.message || error.message);
    }
    console.log('\n' + '='.repeat(60) + '\n');

    // 2. 测试蓝色互补色搭配
    console.log('2️⃣ 测试蓝色互补色搭配...');
    try {
      const blueCompRes = await api.get('/recommendation/color-based', {
        params: { 
          baseColor: '蓝色', 
          colorScheme: 'complementary' 
        }
      });
      
      console.log(`✅ 蓝色互补推荐成功，获得 ${blueCompRes.data.data.recommendations?.length || 0} 个推荐`);
      
      if (blueCompRes.data.data.recommendations?.length > 0) {
        const firstRec = blueCompRes.data.data.recommendations[0];
        console.log(`\n   第一个推荐: ${firstRec.name || '无名称'}`);
        console.log(`   推荐理由: ${firstRec.reason || '无'}`);
        console.log(`   包含 ${firstRec.items?.length || 0} 件衣服:`);
        
        let hasBlueItem = false;
        firstRec.items?.forEach(item => {
          const category = item.category?.name || '未知分类';
          const isBlue = item.color && item.color.includes('蓝');
          if (isBlue) hasBlueItem = true;
          
          console.log(`     ${isBlue ? '🔵' : '⚪'} ${item.name}(${item.color || '无色'}) [${category}]`);
        });
        
        if (hasBlueItem) {
          console.log(`   ✅ 包含蓝色单品`);
        } else {
          console.log(`   ❌ 没有蓝色单品！`);
        }
      }
    } catch (error) {
      console.error('❌ 蓝色互补推荐失败:', error.response?.data?.message || error.message);
    }
    console.log('\n' + '='.repeat(60) + '\n');

    // 3. 测试智能推荐的品类建议
    console.log('3️⃣ 测试智能推荐的品类建议...');
    try {
      const smartRes = await api.get('/recommendation/smart', {
        params: { 
          city: '北京',
          includeWeather: true,
          includePersonal: true
        }
      });
      
      console.log(`✅ 智能推荐成功，获得 ${smartRes.data.data.recommendations?.length || 0} 个推荐`);
      
      // 检查品类建议
      if (smartRes.data.data.categoryAdvice) {
        console.log(`\n📊 品类建议:`);
        console.log(`   消息: ${smartRes.data.data.categoryAdvice.message}`);
        console.log(`   缺少品类: ${smartRes.data.data.categoryAdvice.missing?.join('、') || '无'}`);
        console.log(`   建议: ${smartRes.data.data.categoryAdvice.suggestions?.join('; ') || '无'}`);
      } else {
        console.log(`\n📊 没有品类建议（可能衣柜已经很完整）`);
      }
      
      // 显示推荐分析
      if (smartRes.data.data.analysis) {
        console.log(`\n📈 推荐分析:`);
        console.log(`   天气推荐: ${smartRes.data.data.analysis.weatherBased || 0} 个`);
        console.log(`   个人偏好: ${smartRes.data.data.analysis.personalBased || 0} 个`);
        console.log(`   场合推荐: ${smartRes.data.data.analysis.occasionBased || 0} 个`);
        console.log(`   AI推荐: ${smartRes.data.data.analysis.aiBased || 0} 个`);
      }
      
    } catch (error) {
      console.error('❌ 智能推荐失败:', error.response?.data?.message || error.message);
    }

    console.log('\n🎉 蓝色推荐和品类建议测试完成！');

  } catch (error) {
    console.error('❌ 测试过程中发生错误:', error.message);
  }
}

// 运行测试
testBlueColorRecommendation();
